package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.OrderService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private OrderService orderService;


    @RequestMapping("/findOrderListByPage")
    public PageResult findOrderListByPage(@RequestBody QueryPageBean queryPageBean) {

        return orderService.findOrderListByPage(queryPageBean);
    }


    @RequestMapping("/findOrderById")
    public Result findOrderById(int id) {
        Map<String, Object> map = orderService.findById(id);
        Date o = (Date) map.get("orderDate");
        String format = new SimpleDateFormat("yyyy-MM-dd").format(o);

        System.out.println(format);
        map.put("orderDate",format);
        return new Result(true, "", map);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Map<String, Object> map) {

        try {
            orderService.edit(map);
            return new Result(true, "编辑订单信息成功");
        } catch (RuntimeException r) {
            return new Result(false, r.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑订单信息失败");
        }
    }

    @RequestMapping("/delOrderById")
    public Result delOrderById(int id) {

        try {
            orderService.delOrderById(id);
            return new Result(true,"删除订单成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }

    }


}
